﻿//<my-matriculator - a simple tool for registering racers in a USAC cyclocross race>
//Copyright (C) <2011>  <Steven Rivers>

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Collections.ObjectModel;
using Registration.Model.Entities;

namespace Registration.Model
{
    class ResultsDb
    {
        public static ICollection<Result> GetAllResults()
        {
            var ds = ResultsDbDataset.ReadDataSet();
            var results = new ObservableCollection<Result>();
            foreach (var e in from DataRow resultrow in ds.Tables["Result"].Rows 
                              select new Result((int)resultrow["OverallPlace"], (int)resultrow["CategoryPlace"], 
                                  (int)resultrow["BibNumber"], (int)resultrow["LicenseNumber"], (int)resultrow["FieldId"], 
                                  (string)resultrow["CategoryName"], (string)resultrow["FirstName"], 
                                  (string)resultrow["LastName"], (string)resultrow["Club"]))
            {
                results.Add(e);
            }
            return results;
        }

        public static int GetPlaceInCategory(int fieldid, string category)
        {
            return 1 + GetResultsForField(fieldid).Count(r => r.CategoryName == category);
        }

        public static void AddResult(Result result)
        {
            DataSet ds = ResultsDbDataset.ReadDataSet();
            DataRow row = ds.Tables["Result"].NewRow();
            row.SetField("OverAllPlace", result.Place);
            row.SetField("CategoryPlace", result.CategoryPlace);
            row.SetField("BibNumber", result.BibNumber);
            row.SetField("FieldId", result.FieldId);
            row.SetField("CategoryName", result.CategoryName);
            row.SetField("LicenseNumber", result.License);
            row.SetField("FirstName", result.FirstName);
            row.SetField("LastName", result.LastName);
            row.SetField("Club", result.Club);
            ds.Tables["Result"].Rows.Add(row);
            ResultsDbDataset.WriteDataSet(ds);
        }

        public static IList<Result> GetResultsForField(int field)
        {
            var results = GetAllResults();
            var matches = from result in results
                                          where result.FieldId == field
                                          select result;
            return matches.ToList();
        }

        public static bool IsAlreadyInField(int field, Result result)
        {
            var results = GetResultsForField(field);

            var matches = from r in results
                                          where r.BibNumber == result.BibNumber
                                          select result;

            return (matches.ToList().Count != 0);
        }

        public static void DeleteLast()
        {
            var  ds = ResultsDbDataset.ReadDataSet();
            int count = ds.Tables["Result"].Rows.Count;
            if (count <= 0) return;
            ds.Tables["Result"].Rows[count - 1].Delete();
            ds.AcceptChanges();
            ResultsDbDataset.WriteDataSet(ds);
        }
    }
}
